import { List, getCounties, Reasons } from './store'

const { result } = await List()
let obj = {
    ageDistribution: [],
    populationSize: [],
    povertyComposition: []
} as any

result.forEach((el: any) => {
    el.ageDistribution = JSON.parse(el.ageDistribution)
    el.populationSize = JSON.parse(el.populationSize)
    el.povertyComposition = JSON.parse(el.povertyComposition)
})

function getArr(result: any, key: any) {
    result.forEach((el: any) => {
        el[key].forEach((item: any) => {
            let target = obj[key].find((items: any) => items.label == item.label)
            if (target) {
                target.value = +target.value + +item.value
            } else {
                obj[key].push({
                    label: item.label,
                    value: +item.value
                })
            }
        })
    })
}

getArr(result, 'ageDistribution')
getArr(result, 'populationSize')
getArr(result, 'povertyComposition')

export const resObject = obj
export const populationSize = obj.populationSize.map((item: any) => {
    return {
        name: item.label,
        value: item.value
    }
})

const { result: res } = await getCounties()
export const counties = res[0]

const { result: raeson } = await Reasons()
export const reasons = raeson
